#include "precomp.h"
#include "../Entity/Entity.h"
#include "../Map/Grid.h"

namespace Tmpl {


#define SPHERE_SEGMENTS 8
static Vec2f gSpherePos[SPHERE_SEGMENTS];

void Render::Init()
{
	float step = (360.0f / float(SPHERE_SEGMENTS)) * (PI / 180.0f);
	for (int i=0; i<SPHERE_SEGMENTS; ++i)
	{
		float s = float(i)*step;
		gSpherePos[i] = Vec2f(cosf(s), sinf(s));
	}
}
void Render::RenderModel(int type, Vec2f pos, Vec2f facing, float size)
{
	float rSize = size / 1.41f;

	switch (type)
	{
	case Entity::ENTITY_UNIT:
		// Draw Entity
		glColor3f(0.5f,0.5f,1.0f);
		for (int i=0; i<SPHERE_SEGMENTS; ++i)
		{
			glVertex2f(pos.x + size * gSpherePos[i].x, pos.y + size * gSpherePos[i].y);
		}

		// Facing Line
		glColor3f(0,1,0); glVertex2f(pos.x, pos.y);
		glColor3f(1,0,0); glVertex2f(pos.x + facing.x * size, pos.y + facing.y * size * 2.0f);
		break;
	case Entity::ENTITY_HERO:
		// Draw Entity
		glColor3f(1.0f,1.0f,1.0f);
		for (int i=0; i<SPHERE_SEGMENTS; ++i)
		{
			glVertex2f(pos.x + size * gSpherePos[i].x, pos.y + size * gSpherePos[i].y);
		}

		// Facing Line
		glColor3f(0,1,0); glVertex2f(pos.x, pos.y);
		glColor3f(1,0,0); glVertex2f(pos.x + facing.x * size, pos.y + facing.y * size * 2.0f);
		break;
	case Entity::ENTITY_TREE:
		RenderLine2D(Vec2f(pos.x-rSize, pos.y-rSize), Vec2f(pos.x+rSize, pos.y+rSize), Vec3f(0.3f, 1.0f, 0.3f));
		RenderLine2D(Vec2f(pos.x+rSize, pos.y-rSize), Vec2f(pos.x-rSize, pos.y+rSize), Vec3f(0.3f, 1.0f, 0.3f));
		RenderLine2D(Vec2f(pos.x      , pos.y-size ), Vec2f(pos.x      , pos.y+size ), Vec3f(0.3f, 1.0f, 0.3f));
		RenderLine2D(Vec2f(pos.x-size , pos.y      ), Vec2f(pos.x+size , pos.y      ), Vec3f(0.3f, 1.0f, 0.3f));
		break;
	case Entity::ENTITY_DOODADD:
		RenderLine2D(Vec2f(pos.x-size, pos.y-size), Vec2f(pos.x+size, pos.y+size), Vec3f(1.0f, 0.3f, 0.3f));
		RenderLine2D(Vec2f(pos.x+size, pos.y-size), Vec2f(pos.x-size, pos.y+size), Vec3f(1.0f, 0.3f, 0.3f));
		break;
	case Entity::ENTITY_PROJECTILE:
		glColor3f(0.5f,0.5f,0.0f);
		glVertex2f(pos.x, pos.y - size);
		glVertex2f(pos.x + size, pos.y);

		glVertex2f(pos.x + size, pos.y);
		glVertex2f(pos.x, pos.y + size);

		glVertex2f(pos.x, pos.y - size);
		glVertex2f(pos.x - size, pos.y);

		glVertex2f(pos.x - size, pos.y);
		glVertex2f(pos.x, pos.y + size);
		break;
	};
}

void Render::RenderQuad(Vec2f tl, Vec2f br, Vec3f color)
{
	glColor3fv(color);
	glVertex2f(tl.x, tl.y); 
	glVertex2f(br.x, tl.y);
	glVertex2f(br.x, br.y); 
	glVertex2f(tl.x, br.y);
}
void Render::RenderQuadOutlined(Vec2f tl, Vec2f br, Vec3f color, float borderSize, Vec3f borderColor)
{
	glBegin(GL_QUADS);
		RenderQuad(tl, br, color);
	glEnd();

	glColor3fv(borderColor);
	glLineWidth(borderSize);
	glBegin(GL_LINE_LOOP);
		glVertex2f(tl.x, tl.y); 
		glVertex2f(br.x, tl.y); 
		glVertex2f(br.x, br.y); 
		glVertex2f(tl.x, br.y); 
	glEnd();
	glLineWidth(1.0f);
}

void Render::RenderLine2D(Vec2f start, Vec2f end, Vec3f color)
{
	glColor3fv(color);
	glVertex2fv(start);
	glVertex2fv(end);
}

void Render::RenderHealthbar(Vec2f pos, float entSize, float health)
{
	Vec3f full = Vec3f(0,1,0);
	Vec3f emtpy = Vec3f(0,0,0);

	float x = pos.x; 
	float y = pos.y + entSize;
	float h = 0.05f;

	// background
	glColor3f(0,0,0);
	glVertex2f(x-entSize, y-h);
	glVertex2f(x+entSize, y-h);
	glVertex2f(x+entSize, y+h);
	glVertex2f(x-entSize, y+h);

	// forground
	float progress = health * 2.0f;
	glColor3f(0,1,0);
	glVertex2f(x-entSize					, y-h);
	glVertex2f(x-entSize+entSize*progress	, y-h);
	glVertex2f(x-entSize+entSize*progress	, y+h);
	glVertex2f(x-entSize					, y+h);
}


}; // namespace Tmpl